/*
 * Copyright (c) 2016 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShCompanyLaundryDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShCompanyLaundryService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShCompanyLaundry;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.domain.vo.boss.Tree;

/**
 * ShCompanyLaundryService 实现类
 * @author KongXiangyu
 * @since 2016-03-21
 */
@Service("shCompanyLaundryService")
public class ShCompanyLaundryServiceImpl extends BaseServiceImpl<ShCompanyLaundry, Long> implements ShCompanyLaundryService {

    @Resource
    private ShCompanyLaundryDao shCompanyLaundryDao;

    public BaseDao<ShCompanyLaundry, Long> getDao() {
        return shCompanyLaundryDao;
    }

	@Override
	public DataGrid quetyDataGrid(ShCompanyLaundry companyLaundry, PageHelper page) {
		DataGrid grid = new DataGrid();

		

		int startIndex = (page.getPage() - 1) * page.getRows();
		int endIndex = page.getPage() * page.getRows();
		Long count = shCompanyLaundryDao.queryCompanyLaundryCount(companyLaundry);
		grid.setTotal(count);

		if (grid.getTotal() > 0 && grid.getTotal() > startIndex) {
			companyLaundry.setStartIndex(startIndex);
			companyLaundry.setEndIndex(endIndex);
			if (!StringUtils.isBlank(page.getOrder())) {
				companyLaundry.setOrderFieldType(page.getOrder());
			}
			if (!StringUtils.isBlank(page.getSort())) {
				companyLaundry.setOrderField(page.getSort());
			}
			List<ShCompanyLaundry> rows = shCompanyLaundryDao.queryCompanyLaundry(companyLaundry);
			grid.setRows(rows);
		}

		return grid;
	}
	
	@Override
	public Tree getTree(ShCompanyLaundry companyLaundry) {
		List<ShCompanyLaundry> companyLaundryList = this.selectEntryList(companyLaundry);
		List<Tree> companyLaundryTrees = new ArrayList<Tree>();
		int length = companyLaundryList.size();
		Tree companyLaundryTree = null;
		ShCompanyLaundry express = null;
		for(int i = 0;i < length;i++) {
			express = companyLaundryList.get(i);
			companyLaundryTree = new Tree(express.getId(), express.getName(), 0l);
			companyLaundryTrees.add(companyLaundryTree);
		}
		Tree root = new Tree(0l, "洗衣店");
		root.setChildren(companyLaundryTrees);
		return root;
	}

}